package com.sap.apitest2.deprecated.process;

import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.sap.apitest2.entity.Result;


public class ResultProcess extends EntityProcess {

	public Collection<Result> getRunResultsBySubCase(Long id)throws Exception{
		EntityManager em = this.getEntityManager();
		try{			
			Query q = em.createQuery("SELECT p FROM "+Result.class.getName()+" p WHERE P.subCaseId=?1 ORDER BY p.date DESC");
			q.setParameter(1, id);
			
		    @SuppressWarnings("unchecked")
			List<Result> results = q.getResultList();
		    for (Result res: results) {
		    	if (res.getResponse() != null) {
		    		int length = res.getResponse().length();
		    		System.out.println(length);
		    	}
		    	em.detach(res);
		    }
			return results;
		}
		catch(Exception ex){
			throw ex;//this.LogException(ex);
		}
		finally{
			this.releaseEntityManager(em);
		}
	}
	
	public Result getRecentlyRunResultsByStep(Long subcase_id, Long step_id)throws Exception{
		EntityManager em = this.getEntityManager();
		try{			
			Query q = em.createQuery("SELECT p FROM "+Result.class.getName()+" p WHERE P.subCaseId=?1 AND P.stepId=?2 ORDER BY p.date DESC");
			q.setParameter(1, subcase_id);
			q.setParameter(2, step_id);
			
		    @SuppressWarnings("unchecked")
			List<Result> results = q.getResultList();			
			if(results.size()>0) return results.get(0);
		}
		catch(Exception ex){
			throw ex;//this.LogException(ex);
		}
		finally{
			this.releaseEntityManager(em);
		}
		return null;
	}
}
